package playnsimple.core.graphics;

import static playn.core.PlayN.graphics;

import org.jbox2d.common.Vec2;

import playn.core.CanvasImage;
import playn.core.Font;
import playn.core.ImageLayer;
import playn.core.TextFormat;
import playn.core.TextLayout;

public class TextImage {

    public String text;
    public Vec2 pt = new Vec2(0,0);
    
    // font settings
    public String fontName = GraphicsApi.SANS_SERIF;
    public Integer fontSize = 10;
    public Integer fontColor = GraphicsApi.WHITE;
    public Font.Style fontStyle = Font.Style.PLAIN;
    
    public Integer padding = 2;
    
    
    public TextImage(String text) {
        this.text = text;
    }
    
    public ImageLayer render() {
        CanvasImage image = getImage();
        return graphics().createImageLayer(image);
    }
    
    public CanvasImage getImage() {
        Font font = graphics().createFont(fontName, fontStyle, fontSize);
        TextFormat fontFormat = new TextFormat().withFont(font);
        
        TextLayout layout = graphics().layoutText(text, fontFormat);
        Integer width = (int) layout.width() + padding * 2;
        Integer height = (int) layout.height() + padding * 2;
        
        CanvasImage textImage = graphics().createImage(width, height);
        textImage.canvas().setFillColor(fontColor);
        textImage.canvas().fillText(layout, padding, padding);
        
        return textImage;
    }
    
    public TextImage font(String fontName, Integer fontSize, Integer fontColor) {
        this.fontName = fontName;
        this.fontSize = fontSize;
        this.fontColor = fontColor;
        return this;
    }
    
    public TextImage wrap() {
        return this;
    }
    
    
    
    
    
}
